package com.njiandan.johnson.vr360library.filters.base;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;

import com.njiandan.johnson.vr360library.constant.AdjustingMode;
import com.njiandan.johnson.vr360library.object.Plain;
import com.njiandan.johnson.vr360library.textures.BitmapTexture;
import com.njiandan.johnson.vr360library.utils.MatrixUtils;
import com.njiandan.johnson.vr360library.utils.TextureUtils;

/**
 * author：johnsonZhang on 2017/3/29 09:49
 * email：15915356@qq.com
 */
public class DrawImageFilter extends PassThroughFilter {
    private Plain imagePlain;
    private BitmapTexture bitmapTexture;
    private String imagePath;
    private int adjustingMode;

    public DrawImageFilter(Context context, String imagePath, int adjustingMode) {
        super(context);
        bitmapTexture = new BitmapTexture();
        this.imagePath = imagePath;
        imagePlain = new Plain(false);
        this.adjustingMode = adjustingMode;
    }

    @Override
    public void init() {
        super.init();
        bitmapTexture.load(context, imagePath);
    }

    @Override
    public void onDrawFrame(int textureId) {
        //If we want to draw the image fullscreen,
        //the previous texture can be ignored.
        if (adjustingMode != AdjustingMode.ADJUSTING_MODE_STRETCH) {
            super.onDrawFrame(textureId);
        } else {
            onPreDrawElements();
            glPassThroughProgram.use();
            Matrix.setIdentityM(projectionMatrix, 0);
            GLES20.glViewport(0, 0, surfaceWidth, surfaceHeight);
        }
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
        TextureUtils.bindTexture2D(bitmapTexture.getImageTextureId(), GLES20.GL_TEXTURE0, glPassThroughProgram.getTextureSamplerHandle(), 0);
        imagePlain.uploadTexCoordinateBuffer(glPassThroughProgram.getTextureCoordinateHandle());
        imagePlain.uploadVerticesBuffer(glPassThroughProgram.getPositionHandle());
        MatrixUtils.updateProjection(
                bitmapTexture.getImageWidth(),
                bitmapTexture.getImageHeight(),
                surfaceWidth,
                surfaceHeight,
                adjustingMode,
                projectionMatrix);
        GLES20.glUniformMatrix4fv(glPassThroughProgram.getMVPMatrixHandle(), 1, false, projectionMatrix, 0);
        imagePlain.draw();
        GLES20.glDisable(GLES20.GL_BLEND);
    }

    @Override
    public void onFilterChanged(int surfaceWidth, int surfaceHeight) {
        super.onFilterChanged(surfaceWidth, surfaceHeight);
    }
}
